package com.city.vodservice.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.city.vodservice.service.VodService;
import com.city.vodservice.utils.ConstantVodUtils;
import com.city.vodservice.utils.InitVodCilent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Cheng
 * Date: 2020/9/10
 * Time: 12:11
 * Description: No Description
 */
@Service
public class VodServiceImpl implements VodService {

    //视频文件上传到阿里云
    @Override
    public String uploadVideoAly(MultipartFile file) {
        try {
            String fileName=file.getOriginalFilename();
            //substring获取最后一个“字符”得前面值
            String title=fileName.substring(0,fileName.lastIndexOf("."));
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest uploadStreamRequest = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET,title,fileName,inputStream);
            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(uploadStreamRequest);
            String videold=null;
            if (response.isSuccess()){
                videold=response.getVideoId();
            }else {
                videold=response.getVideoId();
            }
            return videold;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void removeAlyVideoById(String id) {
        try {
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            deleteVideoRequest.setVideoIds(id);
            client.getAcsResponse(deleteVideoRequest);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void removeMoreAlyVideo(List videoIdList) {
        try {
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();

            String ids=null;
            /*for (Object o : videoIdList) {
                ids=ids+","+o;
            }*/

            ids= StringUtils.join(videoIdList.toArray(),",");//import org.apache.commons.lang.StringUtils;

            deleteVideoRequest.setVideoIds(ids);
            client.getAcsResponse(deleteVideoRequest);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
